package com.hskn.hss.utils.redis;

import com.alibaba.fastjson.JSON;
import com.hskn.hss.utils.JedisPoolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class RedisUtil {

    private static Jedis getJedis() {
        return JedisPoolUtils.getJedis();
    }

    /**
     * 获取当个对象
     */
    public static <T> T get(KeyPrefix prefix, String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            String str = jedis.get(realKey);
            T t = stringToBean(str, clazz);
            return t;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 设置对象
     */
    public static <T> boolean set(KeyPrefix prefix, String key, T value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            String str = beanToString(value);
            if (str == null || str.length() <= 0) {
                return false;
            }
            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            int seconds = prefix.expireSeconds();
            if (seconds <= 0) {
                jedis.set(realKey, str);
            } else {
                jedis.setex(realKey, seconds, str);
            }
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 设置 list 对象
     */
    public static <T> boolean setList(KeyPrefix prefix, String key, T value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            String str = beanToString(value);
            if (str == null || str.length() <= 0) {
                return false;
            }
            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            int seconds = prefix.expireSeconds();
            jedis.rpush(realKey, str);
            if (seconds > 0) {
                jedis.expire(realKey, seconds);
            }
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 获取所有 list 对象
     */
    public static <T> List getAllList(KeyPrefix prefix, String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            List<String> list = jedis.lrange(realKey, 0, -1);
            if (CollectionUtils.isEmpty(list)) {
                return null;
            }
            List<T> listT = new ArrayList<>();
            for (String s : list) {
                T t = stringToBean(s, clazz);
                listT.add(t);
            }
            return listT;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 删除对象
     */
    public static boolean del(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            jedis.del(realKey);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 判断key是否存在
     */
    public static <T> boolean exists(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            //生成真正的key
            String realKey = prefix.getPrefix() + key;
            return jedis.exists(realKey);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        } finally {
            returnToPool(jedis);
        }
    }

    private static <T> String beanToString(T value) {
        if (value == null) {
            return null;
        }
        Class<?> clazz = value.getClass();
        if (clazz == int.class || clazz == Integer.class) {
            return "" + value;
        } else if (clazz == String.class) {
            return (String) value;
        } else if (clazz == long.class || clazz == Long.class) {
            return "" + value;
        } else {
            return JSON.toJSONString(value);
        }
    }

    @SuppressWarnings("unchecked")
    private static <T> T stringToBean(String str, Class<T> clazz) {
        if (str == null || str.length() <= 0 || clazz == null) {
            return null;
        }
        if (clazz == int.class || clazz == Integer.class) {
            return (T) Integer.valueOf(str);
        } else if (clazz == String.class) {
            return (T) str;
        } else if (clazz == long.class || clazz == Long.class) {
            return (T) Long.valueOf(str);
        } else if (clazz == List.class) {
            return JSON.toJavaObject(JSON.parseArray(str), clazz);
        } else {
            return JSON.toJavaObject(JSON.parseObject(str), clazz);
        }
    }

    private static void returnToPool(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}
